<?php
/**
 * APP端用户 api
 * User: xwc
 * Date: 2023/3/25 17:28
 */

namespace App\OpenApi\AppApi\User;

use App\Library\ThirdParty\ZuoYi\Text;
use App\Library\ThirdParty\ZuoYi\ZuoYi;
use App\Model\Core\Doctor\BadJsModel;
use App\Model\Core\Doctor\MessageModel;
use App\Service\AppPush\DeviceBindService;
use App\Service\Core\Doctor\UserClientService;
use App\Service\Core\Service\ZuoYi\ZuoYiChatService;
use App\Service\Core\User\User\UserService;
use App\Service\Core\User\User\UserStoreApiService;
use App\Service\OpenApi\AppApi\OpenBaseApi;

class UserApi extends OpenBaseApi
{
    /**
     * 注销账户
     * @return array
     */
    public function close(): array
    {
        $close_res = UserService::getInstance()->close($this->operate_id, $this->operate_by);
        if ($close_res['code'] === 0) {
            //注销成功，强制退出登录
            (new \App\Service\OpenApi\AppApi\AppAuthService($this->app_key))->cancelAuth($this->token);
            //解绑设备
            (new DeviceBindService($this->app_key))->appLoginOut($this->operate_id, $this->device_id);
        }
        return $close_res;
    }

    /**
     * 用户信息是否完善
     * @return array
     */
    public function isComplete(): array
    {
        $is_complete = UserService::getInstance()->checkUserInfoComplete($this->operate_id);
        return alert_info(0, 'OK', ['is_complete' => $is_complete]);
    }

    /**
     * 补全用户信息
     * @return array
     * @throws \Throwable
     */
    public function store()
    {
        $UserStoreApiService = new UserStoreApiService();
        return $UserStoreApiService
            ->setUsername($this->input('username'))
            ->setSex($this->input('sex'))
            ->setBirthday($this->input('birthday'))
            ->setOperate($this->operate_id, $this->operate_by)
            ->store($this->operate_id);
    }

    /**
     * 获取用户信息
     * @return array
     */
    public function info()
    {
        $type = $this->input('type');
        if ($type == 'online') { // 状态
            $service = UserClientService::getInstance();
            $client_id = $this->input('client_id');
            $result = $service->setUserOnline($this->operate_id, $client_id, $this->operate_by);
            return $result;
        } else if ($type == 'badjs') {
            $request = request();
            $msg = $request->input('msg');
            $url = $request->input('url');
            $error = $request->input('error');
            $line = (int)$request->input('line');
            $col = (int)$request->input('col');
            if (empty($msg) && empty($error)) {
                return alert_info(1, '消息为空');
            }
            BadJsModel::getInstance()->bad($msg, $error, $url, $line, $col);
            return alert_info(0, 'ok');
        } else {
            $UserService = UserService::getInstance();
            $user_info = $UserService->getOne($this->operate_id);
            $user_info = $UserService->getExtInfo($user_info);
            return alert_info(0, 'ok', $user_info);
        }
    }

    /**
     * 一句话导诊
     */
    public function message():array
    {
        $txt = $this->input('txt');
        if (empty($txt)) {
            return alert_info(1, '请输入问题');
        }

        $ZuoYiChatService = ZuoYiChatService::getInstance();
        $chat_type = 1;
        $chat_id_res = $ZuoYiChatService->getChatId($chat_type, $this->operate_id);
        if ($chat_id_res['code'] !== 0) {
            return $chat_id_res;
        }
        $chat_id = $chat_id_res['data']['chat_id'];
        return $ZuoYiChatService->aiChat($chat_id, $chat_type, $this->operate_id, $txt);
//        $zy = new ZuoYi();
//        $resp = $zy->chat($txt);
//
//        if ($resp['code'] != 0) {
//            return alert_info(1, $resp['message']);
//        }
//
//        $choices = $resp['data']['choices'];
//        $parse_res = Text::parses($choices);
//        $reply = [];
//        if ($parse_res['code'] == 0) {
//            $reply = json_encode($parse_res['data']);
//        }
//        // 日志
//        $user_id = $this->operate_id;
//        MessageModel::getInstance()->question($user_id, $txt, $reply);
//        MessageModel::getInstance()->reply($user_id, $reply, '');
//        return $parse_res;
    }
}
